﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPIDesktopClient.Entities;
using PPIDesktopClient.General;
using VMind.Global;

namespace PPIDesktopClient.Controls.MobileDatabaseTree
{
    public partial class MobileDatabaseTree : UserControl
    {

        private EMobileDatabase m_EMobileDatabaseTree;
        public MobileDatabaseTree()
        {
            InitializeComponent();
        }
        private string m_ConnectionString;
        public string ConnectionString
        {
            get
            {
                return m_ConnectionString;
            }
            set
            {
                m_ConnectionString = value;
            }
        }

        #region public Properties
        public TreeNodeEx SelectedItem
        {
            get { return (TreeNodeEx)tvwMain.SelectedNode; }
            set { tvwMain.SelectedNode = value; }
        }
        #endregion

        #region Public Methods

        protected void AddNodes()
        {
            TreeNodeEx _Parent;
            TreeNodeEx[] _Childrens;
            try
            {
                _Childrens = GetChildNodes(m_EMobileDatabaseTree.Assets);
                _Parent = new TreeNodeEx(m_EMobileDatabaseTree.ConnectionString, "Mobile Database", _Childrens);
                tvwMain.Nodes.Add(_Parent);
            }
            catch (Exception)
            {

                throw;
            }
        }
        private TreeNodeEx[] GetChildNodes(List<EVendor_Visit_XMissionStructure> assets)
        {
            TreeNodeEx[] _Childrens;
            _Childrens = new TreeNodeEx[assets.Count];
            for (int i = 0; i < assets.Count; i++)
            {
                _Childrens[i] = new TreeNodeEx(assets[i].Id.ToString());
                _Childrens[i].Name = assets[i].Id.ToString();
                _Childrens[i].Value = assets[i].ConnectionString;
            }
            return _Childrens;
        }
        void CheckChildren(TreeNodeEx parentNode)
        {
            foreach (TreeNodeEx _Node in parentNode.Nodes)
            {
                _Node.Checked = parentNode.Checked;
                if (_Node.Nodes.Count > 0)
                    CheckChildren(_Node);
            }
        }
        public override void Refresh()
        {
            //Suppress repainting the TreeView until all the objects have been created.
            tvwMain.BeginUpdate();
            //Clear the TreeView each time the method is called.
            tvwMain.Nodes.Clear();

            m_EMobileDatabaseTree = new EMobileDatabase(m_ConnectionString);
            AddNodes();

            //Begin repainting the TreeView.
            tvwMain.EndUpdate();
        }

        #endregion

        #region Events
        public event TreeViewEventHandler CheckedChange;
        public event EventHandler ClickedNew;
        public event EventHandler ClickedEdit;
        public event EventHandler ClickedDelete;
        public event EventHandler SelectedNode;
        public event EventHandler DeselectedNode;


        private void OnCheckedChanged(object child, TreeViewEventArgs e)
        {
            // Safely invoke an event:
            if (CheckedChange != null)
                CheckedChange(child, e);
        }
        private void OnClickedNew(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedNew != null)
            {
                ClickedNew(o, e);
            }
        }
        private void OnClickedEdit(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedDelete(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }
        private void OnSelectedNode(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (SelectedNode != null)
            {
                SelectedNode(o, e);
            }
        }
        private void OnDeselectedNode(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (DeselectedNode != null)
            {
                DeselectedNode(o, e);
            }
        }

        private void tvwMain_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            AfterCheck((TreeNodeEx)e.Node);
        }

        protected virtual void AfterCheck(TreeNodeEx _Node)
        {
            if (_Node.Level > 0)
            {
                foreach (EVendor_Visit_XMissionStructure a in m_EMobileDatabaseTree.Assets)
                {
                    if (a.Id == ConvertHelper.ToInt32(_Node.Name))
                        OnCheckedChanged(a, new TreeViewEventArgs(_Node));
                }
            }
            CheckChildren(_Node);

        }

        private void cmiNewAsset_Click(object sender, EventArgs e)
        {
            OnClickedNew(sender, e);
        }

        private void cmiEditAsset_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, e);
        }

        private void cmiDeleteAsset_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, e);
        }

        private void tvwMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNodeEx node = (TreeNodeEx)e.Node;
            if (node != null && node.Level != 0)
            {
                tvwMain.SelectedNode = node;
                OnSelectedNode(sender, e);
            }
            else
            {
                OnDeselectedNode(sender, e);
            }
        }
        private void tvwMain_Leave(object sender, EventArgs e)
        {
            tvwMain.SelectedNode = null;
            OnDeselectedNode(sender, e);
        }
        #endregion
    }
}
